import pandas as pd

def sma(df, ma=(5,10,20,30,60,120,150)):
    for period in ma:
        df[f'ma{period}'] = df['close'].rolling(window=period).mean()
    return df

def ema(df, ema=(5,10,20,30,60,120,150)):
    for period in ema:
        df[f'ema{period}'] = df['close'].ewm(span=period, adjust=False).mean()
    return df

def sma_cross(df, ma1=5, ma2=10):
    df = sma(df, (ma1, ma2))
    list_cross = []
    for index in range(len(df)):
        if index == 0:
            continue
        if df.iloc[index-1][f'ma{ma1}'] > df.iloc[index-1][f'ma{ma2}'] and df.iloc[index][f'ma{ma1}'] < df.iloc[index][f'ma{ma2}']:
            list_cross.append((index, 'dead'))
        elif df.iloc[index-1][f'ma{ma1}'] < df.iloc[index-1][f'ma{ma2}'] and df.iloc[index][f'ma{ma1}'] > df.iloc[index][f'ma{ma2}']:
            list_cross.append((index, 'gold'))
        else:
            pass
    return list_cross

def sma_price_cross(df, ma=5, day=5, threasold=0.001):
    '''
    连续day天，低于ma线的价格，并于今日突破ma*threasold，则视为金叉，反正设为死叉
    '''
    pass

def sma_trend(df, ma=5, day=20, threasold=-2):
    '''
    判断ma基于day天的趋势，threasold为阈值，大于0表示向上趋势，小于0表示向下趋势（-2为下跌趋势明显，+2为上涨趋势明显）
    '''
    pass